using System;
using System.Security.Cryptography;
using System.Text;

namespace LiJ.Common.Encrypt
{
	/// <summary>
	/// Sha1加密类
	/// </summary>
	public static class Sha1
	{
		/// <summary>
		/// 获取Sha1加密值
		/// </summary>
		/// <param name="input"></param>
		/// <param name="encoding"></param>
		/// <returns></returns>
		public static string Encrypt(string input, Encoding encoding = null)
		{
			if (string.IsNullOrEmpty(input))
			{
				throw new ArgumentNullException("input", "Sha1加密的字符串不能为空！");
			}
			if (encoding == null)
			{
				encoding = Encoding.UTF8;
			}
			byte[] array = Encrypt(encoding.GetBytes(input));
			StringBuilder stringBuilder = new StringBuilder(array.Length * 2);
			byte[] array2 = array;
			foreach (var b in array2)
			{
			    stringBuilder.Append(b.ToString("x2"));
			}
			return stringBuilder.ToString();
		}

		/// <summary>
		/// 获取Sha1加密值
		/// </summary>
		/// <param name="bytes"></param>
		/// <returns></returns>
		public static byte[] Encrypt(byte[] bytes)
		{
		    if (bytes == null || bytes.Length == 0) throw new ArgumentNullException("bytes", "Sha1加密的字节不能为空！");
		    using (var sHa = SHA1.Create())
		    {
		        return sHa.ComputeHash(bytes);
		    }
		}
	}
}
